<?php
namespace app\crm\controller;
use think\Loader;

class Outboundmanager extends CrmController
{   
    /**
     * 加载外呼任务列表模板
     * @return [type] [description]
     */
    public function outboundTaskList()
    {	
		//分配增删改的权限
		$menuname = "Outbound Task List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		// 模板输出
        return $this->fetch('outboundTaskList');
    }
	

    public function callRecord()
    {
		//分配增删改的权限
		$menuname = "Outbound Task List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$this->assign("priv",$priv);
		// print_r($_REQUEST['id']);exit;
		$this->assign("id",$_REQUEST['id']);
		// 模板输出
		return $this->fetch('callRecord');
    }

	public function saleSourceData()
    {
    	$where = "";
		// 模板输出
        $arrData = Loader::model('Outboundmanager')->saleSourceData($where);
        echo json_encode($arrData);
    }
	
	public function outboundTaskData()
    {
		// 模板输出
        $arrData = Loader::model('Outboundmanager')->outboundTaskData();
        echo json_encode($arrData);
    }
	
	public function addTask()
	{
		//分配增删改的权限
		$menuname = "Outbound Task List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$this->assign("priv",$priv);

		$result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);
		$tpl_ivr = Loader::model('Outboundmanager')->getTaskIVR();
		
		$this->assign('ivr_id',$tpl_ivr);
		$tpl_trunks = Loader::model('Outboundmanager')->getTaskTrunks();
		$this->assign('trunk',$tpl_trunks);
		$arrUData = Loader::model('Outboundmanager')->getTaskUsers();
		$str="";
		foreach($arrUData as $k=>$v){
			$str.="<input name='join_queue_method2[]' type='checkbox' value='".$v['username']."' title='".$v['cn_name']."'>";
		}
		$this->assign('arrUData',$str);
		return $this->fetch('addTask');
	}
	
	public function insertTask()
	{
		Loader::model('Outboundmanager')->insertTask();
	}

	public function updateTask()
	{
		Loader::model('Outboundmanager')->updateTask();
	}
	
	public function taskDetail()
	{
		//分配增删改的权限
		$menuname = "Outbound Task List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$this->assign("priv",$priv);
		
		$id = $_REQUEST["id"];
		$this->assign('id',$id);
		$tpl_ST = Loader::model('Outboundmanager')->dialManager($id);


		if( file_exists("/var/tmp/autocall/bgcrm:$id") ){
			$tpl_ST['status'] = "on";
			$tpl_ST['concurrence'] = file_get_contents("/var/tmp/autocall/bgcrm:$id");
		}else{
			$tpl_ST['status'] = "off";
		}
		$this->assign("ST",$tpl_ST);
		
		return $this->fetch('taskDetail');
	}
	
	public function editTask()
	{
		$result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);
		$tpl_ivr = Loader::model('Outboundmanager')->getTaskIVR();
		$this->assign('ivr_id',$tpl_ivr);
		$tpl_trunks = Loader::model('Outboundmanager')->getTaskTrunks();
		$this->assign('trunk',$tpl_trunks);
		
		$id = $_REQUEST["id"];
		$taskData = Loader::model('Outboundmanager')->getTaskData($id);
		
		//自动外呼应答目标
		$taskData['radio_queue'] = $taskData['radio_ivr'] = "";
		if( $taskData['answer_target'] == "queue" ){
			$taskData['radio_queue'] = 'checked';
		}elseif($taskData['answer_target'] == "ivr_hungup"){
			$taskData['radio_hungup'] = 'checked';
		}else{
			$taskData['radio_ivr'] = 'checked';
		}
		//坐席加入队列的方式
		$taskData['radio_by_self'] = $taskData['radio_by_admin'] = $taskData['radio_by_DND'] = "";
		if( $taskData['join_queue_method'] == "by-self" ){
			$taskData['radio_by_self'] = 'checked';
		}elseif( $taskData['join_queue_method'] == "by-admin" ){
			$taskData['radio_by_admin'] = 'checked';
		}else{
			$taskData['radio_by_DND'] = 'checked';
		}
		
		$this->assign('taskData',$taskData);
		
		$arrUData = Loader::model('Outboundmanager')->getTaskUsers();
		$str="";
		$arrAgent = explode(",",$taskData["agent_task"]);
		foreach($arrUData as $k=>$v){
			if(in_array($v['username'],$arrAgent)){
				$str.="<input name='join_queue_method2[]' checked type='checkbox' value='".$v['username']."' title='".$v['cn_name']."'>";
			}else{
				$str.="<input name='join_queue_method2[]' type='checkbox' value='".$v['username']."' title='".$v['cn_name']."'>";
			}
		}
		$this->assign('arrUData',$str);
		
		return $this->fetch('editTask');
	}
	
	public function changeDialManager()
	{
		$id = $_REQUEST["id"];
		$status = $_REQUEST["status"];
		$concurrence = $_REQUEST["concurrence"];
		// print_r($_REQUEST);
		$file_contents = file_get_contents('http://192.168.88.59:8888/index.php?m=Telemarketing&a=changeDialManager&id='.$id.'&status='.$status.'&concurrence='.$concurrence);
		// print_r($file_contents);
		if ($file_contents == 'ok'){
			echo json_encode(array('success'=>true,'msg'=>'成功'));
		} else {
			echo json_encode(array('msg'=>'失败'));
		}
		
	}


	public function taskRecordsList()
	{
		$id = $_REQUEST['id'];

		$where = "(1 ";

		$where .= ")";

		// print_r($id);exit;
		$arrData = Loader::model('Outboundmanager')->taskRecordsList($id, $where);
		echo json_encode($arrData);
	}


	/**
	 * 渲染模板transferCustomer
	 */
	public function transferCustomer()
	{
	    $menuname = "Outbound Task List";
	    $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
	    $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
	    $this->assign("priv",$priv);
	    $customer_code = rtrim($_GET['customer_code'], '|');
	    // echo $id;exit;
	    $this->assign('customer_code', $customer_code);
	    $this->assign('id', $_REQUEST['id']);

		$customerCount = Loader::model('Outboundmanager')->customerCount($_REQUEST['id']);
	    $this->assign('customerCount', $customerCount);

	    // 模板输出
	    return $this->fetch('transferCustomer');
	}


	/**
	 * 转交号码池
	 */
	public function doTransferCustomer ()
	{
	    // print_r($_REQUEST);exit;
	    // $customer_code = $_REQUEST['customer_code'];
	    if (empty($_REQUEST['id'])) {
	        echo json_encode(array('msg'=>'操作失败！'));
	        exit;
	    } else {
	        $id = $_REQUEST['id'];
	    }

	    if (empty($_REQUEST['tables'])) {
	        echo json_encode(array('msg'=>'操作失败！'));
	        exit;
	    } else {
	        $tables = 'vip_phone_pool_'.$_REQUEST['tables'];
	    }
	    // $customer_code = join('","',explode('|', $customer_code));
	    // $where = 'customer_code IN ("'.$customer_code.'")';
	    Loader::model('Callplatform')->transferCustomer($id, $tables);
	    // echo json_encode($ids);
	}


	public function resetCallFailedNum ()
	{
	    Loader::model('Outboundmanager')->resetCallFailedNum();
	}

}